var token = localStorage.getItem("key")
var conId = sessionStorage.getItem("conId");
var type = getUrlParam("type")
var switchControl = 0
var lightLevel = ""
var flashPeriod = ""
var startPoint = ""
var flashDura = ""
var brightness = ""
var sensitivity = ""

function backPro(){
	if(type == "all"){
		window.location.href = "projectDetail.html"
	}else{
		window.location.href = "conDetail.html"
	}
}

function getUrlParam(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.slice(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}

function chLight(ele){
	$("p[name='chLight']").css({"color":"rgba(0, 0, 0, 0.6)","background":"rgba(238,238,238,1)"})
	if($(ele).css("color") == "rgba(0, 0, 0, 0.6)"){
		$(ele).css({"color":"white","background":"#B12F41"})
	}else{
		$(ele).css({"color":"rgba(0, 0, 0, 0.6)","background":"rgba(238,238,238,1)"})
	}
	lightLevel = $(ele).html()
}

function chBrightness(ele){
	$("p[name='chBrightness']").css({"color":"rgba(0, 0, 0, 0.6)","background":"rgba(238,238,238,1)"})
	if($(ele).css("color") == "rgba(0, 0, 0, 0.6)"){
		$(ele).css({"color":"white","background":"#B12F41"})
	}else{
		$(ele).css({"color":"rgba(0, 0, 0, 0.6)","background":"rgba(238,238,238,1)"})
	}
	if($(ele).html() == "一"){
		brightness = 1	
	}
	if($(ele).html() == "二"){
		brightness = 2	
	}
	if($(ele).html() == "三"){
		brightness = 3	
	}
	if($(ele).html() == "四"){
		brightness = 4	
	}
	if($(ele).html() == "五"){
		brightness = 5	
	}
	 
}

function verifyNumber(val) {
	var p1 = /^[0-9]*$/;  
    var me = false;  
    if (p1.test(val)) {
    	me=true;
    }
    return me
}

function controllerSetting(){
	if($("#flashPeriod").val()!=""){
		if(verifyNumber($("#flashPeriod").val())){
			flashPeriod = $("#flashPeriod").val()	
		}else{
			mui.toast("请输入数字闪光周期")
			return
		}
	}
	if($("#startPoint").val()!=""){
		if(verifyNumber($("#startPoint").val())){
			startPoint = $("#startPoint").val()
		}else{
			mui.toast("请输入数字闪光起始点")
			return
		}
	}
	if($("#flashDura").val()!=""){
		if(verifyNumber($("#flashDura").val())){
			flashDura = $("#flashDura").val()
		}else{
			mui.toast("请输入数字闪光持续时间")
			return
		}
	}
	if($("#sen1").val()!=""){
		if(!verifyNumber($("#sen1").val())){
			mui.toast("请输入数字光控灵敏度")
			return
		}
	}
	if($("#sen2").val()!=""){
		if(!verifyNumber($("#sen2").val())){
			mui.toast("请输入数字光控灵敏度")
			return
		}
	}
	if($("#sen3").val()!=""){
		if(!verifyNumber($("#sen3").val())){
			mui.toast("请输入数字光控灵敏度")
			return
		}
	}
	if($("#sen4").val()!=""){
		if(!verifyNumber($("#sen4").val())){
			mui.toast("请输入数字光控灵敏度")
			return
		}
	}
	sensitivity = "["+$("#sen1").val()+","+$("#sen2").val()+","+$("#sen3").val()+","+$("#sen4").val()+"]"

	if(type == "all"){
		conId = ""
	}
	$.ajax({
		type: "POST",
		url: url_prefix + "home/controllerSetting",
		data: {
        	request: JSON.stringify({
        		switchControl:switchControl,
				lightLevel:lightLevel,
				flashPeriod:flashPeriod,
				startPoint:startPoint,
				flashDura:flashDura,
				brightness:brightness,
				sensitivity:sensitivity,
				controllerId:conId,
				token:token
			})   
        },
		success: function(data) {
			if(data.code == '200') {
				mui.toast('保存设置成功');
			} else if (data.code == '300'){
				mui.toast("帐号在其他设备上登录")
				window.location.href="fastLogin.html"
			} else {
				mui.toast('保存设置失败');
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {

			mui.toast("连接服务器失败,请稍后重试``````");
		}
	});
}
